package com.xiewz.executor;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 图片位置移动执行器
 * 得到想对路径的Markdown地址
 */
public class ImgMoveExecutor implements Executor {
    private String mdFilePath;
    private String endPath;

    public ImgMoveExecutor(String mdFilePath) {
        this.mdFilePath = mdFilePath;
        String[] split = this.mdFilePath.split("/|\\\\");
        String dir = this.mdFilePath.substring(0, mdFilePath.indexOf(split[split.length - 1]));
        this.endPath = dir + "img" + File.separator;
        // 创建文件夹
        File file = new File(endPath);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    @Override
    public Object doExecutor(String content) {
        System.out.println("执行器传入参数：" + content);


        // 移动文件
        String fileName = moveTotherFolders(content);
        // 没有成功，就不替换
        if (fileName == null) {
            return "";
        }

        // 得到想对路径
        String relativeFileName = "img/" + fileName;

        // 组成Markdown图片string
        content = String.format("![%s](%s)", fileName, relativeFileName);
        return content;
    }

    private String moveTotherFolders(String startPath) {
        try {
            File startFile = new File(startPath);
            File endFile = new File(endPath + startFile.getName());
            if (!startFile.isFile()) {
                System.out.println(startPath + "不是绝对路径的文件!");
                return null;
            }
            if (endFile.exists()) {
                return endFile.getName();
            }

            System.out.println(endPath + startFile.getName());
            if (copyFile(startFile,endFile)) {
                System.out.println(String.format("File is moved successful! %s to %s", startPath, endPath));
                return endFile.getName();
            } else {
                System.out.println(String.format("File is failed to move! %s to %s", startPath, endPath));
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 可能会权限不足，所以用复制，不用移动
     * @param source
     * @param dest
     * @return
     */
    private Boolean copyFile(File source, File dest) {
        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(source);
            output = new FileOutputStream(dest);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) > 0) {
                output.write(buf, 0, bytesRead);
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
